package jd.client.xml.impl;

import java.util.ArrayList;
import java.util.List;

import jd.client.xml.SAXResponseParser;
import jd.entity.DownloadFile;
import jd.entity.DownloadPackage;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * @author Denis Migol
 * 
 */
public class DownloadsParser extends SAXResponseParser<List<DownloadPackage>> {

	private static final String PACKAGE = "package";
	private static final String FILE = "file";

	private DownloadPackage pkg;

	private List<DownloadFile> files;

	@Override
	public void startDocument() throws SAXException {
		entity = new ArrayList<DownloadPackage>();
	}

	// private static Integer parseEta(final String value) {
	// if ("~".equals(value)) {
	// return null;
	// }
	// return Integer.parseInt(value + "");
	// }

	private static String getName(final Attributes attributes, final int index) {
		String name = attributes.getLocalName(index);
		if ((name == null) || name.isEmpty()) {
			name = attributes.getQName(index);
		}
		return name;
	}

	@Override
	public void startElement(final String uri, final String localName, final String qName, final Attributes attributes)
			throws SAXException {
		if (PACKAGE.equalsIgnoreCase(qName)) {
			pkg = new DownloadPackage();

			final int length = attributes.getLength();
			for (int i = 0; i < length; i++) {
				// final String name = attributes.getLocalName(i);
				final String name = getName(attributes, i);
				final String value = attributes.getValue(i);
				if ("package_ETA".equalsIgnoreCase(name)) {
					// pkg.setEta(parseEta(value));
					pkg.setEta(value);
				} else if ("package_linksinprogress".equalsIgnoreCase(name)) {
					pkg.setLinksInProgress(Integer.parseInt(value));
				} else if ("package_linkstotal".equalsIgnoreCase(name)) {
					pkg.setLinksTotal(Integer.parseInt(value));
				} else if ("package_loaded".equalsIgnoreCase(name)) {
					pkg.setLoaded(value);
				} else if ("package_name".equalsIgnoreCase(name)) {
					pkg.setName(value);
				} else if ("package_percent".equalsIgnoreCase(name)) {
					pkg.setPercent(Double.parseDouble(value.replace(',', '.')));
				} else if ("package_size".equalsIgnoreCase(name)) {
					pkg.setSize(value);
				} else if ("package_speed".equalsIgnoreCase(name)) {
					pkg.setSpeed(value);
				} else if ("package_todo".equalsIgnoreCase(name)) {
					pkg.setTodo(value);
				}
			}
			entity.add(pkg);
		} else if (FILE.equalsIgnoreCase(qName)) {
			if (files == null) {
				files = new ArrayList<DownloadFile>();
				pkg.setFiles(files);
			}
			final DownloadFile file = new DownloadFile();

			final int length = attributes.getLength();
			for (int i = 0; i < length; i++) {
				// final String name = attributes.getLocalName(i);
				final String name = getName(attributes, i);
				final String value = attributes.getValue(i);
				if ("file_hoster".equalsIgnoreCase(name)) {
					file.setHoster(value);
				} else if ("file_name".equalsIgnoreCase(name)) {
					file.setName(value);
				} else if ("file_package".equalsIgnoreCase(name)) {
					file.setPkg(value);
				} else if ("file_percent".equalsIgnoreCase(name)) {
					file.setPercent(Double.parseDouble(value.replace(',', '.')));
				} else if ("file_speed".equalsIgnoreCase(name)) {
					file.setSpeed(value);
				} else if ("file_status".equalsIgnoreCase(name)) {
					file.setStatus(value);
				}
			}
			files.add(file);
		}
	}

	@Override
	public void endElement(final String uri, final String localName, final String qName) throws SAXException {
		if (PACKAGE.equalsIgnoreCase(qName)) {
			files = null;
		}
	}
}
